package org.groupg.practice.swingx.demo;

import lombok.Getter;
import lombok.Setter;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.VerticalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

@Getter
@Setter
public class PersonForm extends JDialog {
    private JTextField idField;
    private JTextField nameField;
    private JXDatePicker birthDatePicker;
    private JComboBox<String> genderCombo;
    private JButton saveButton;
    private JButton cancelButton;

    // 回调接口
    public interface FormActionListener {
        void onSave(int id, String name, Date birthDate, String gender);
    }

    private FormActionListener listener;
    private boolean savePerformed = false;

    public PersonForm(JFrame parent) {
        super(parent, "人员信息", true); // 模态对话框
        setSize(400, 300);
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        JXPanel mainPanel = new JXPanel();
        mainPanel.setLayout(new VerticalLayout(10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        initForm(mainPanel);
        add(mainPanel);

        // 添加窗口监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                if (!savePerformed && listener != null) {
                    // 可以在这里处理取消操作
                }
            }
        });
    }

    private void initForm(JXPanel panel) {
        // ID字段
        JPanel idPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        idPanel.add(new JLabel("ID:"));
        idField = new JTextField(5);
        idField.setEditable(false);
        idPanel.add(idField);
        panel.add(idPanel);

        // 姓名字段
        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        namePanel.add(new JLabel("姓名:"));
        nameField = new JTextField(20);
        namePanel.add(nameField);
        panel.add(namePanel);

        // 出生日期
        JPanel datePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        datePanel.add(new JLabel("出生日期:"));
        birthDatePicker = new JXDatePicker();
        birthDatePicker.setFormats("yyyy-MM-dd");
        datePanel.add(birthDatePicker);
        panel.add(datePanel);

        // 性别选择
        JPanel genderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        genderPanel.add(new JLabel("性别:"));
        genderCombo = new JComboBox<>(new String[]{"男", "女", "其他"});
        genderPanel.add(genderCombo);
        panel.add(genderPanel);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 5));
        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");

        saveButton.addActionListener(this::saveAction);
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);
    }

    private void saveAction(ActionEvent e) {
        if (nameField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "姓名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (listener != null) {
            int id = idField.getText().isEmpty() ? -1 : Integer.parseInt(idField.getText());
            listener.onSave(
                id,
                nameField.getText().trim(),
                birthDatePicker.getDate(),
                (String) genderCombo.getSelectedItem()
            );
            savePerformed = true;
        }
        dispose();
    }

    public void setFormActionListener(FormActionListener listener) {
        this.listener = listener;
    }

    public void clearForm() {
        idField.setText("");
        nameField.setText("");
        birthDatePicker.setDate(new Date());
        genderCombo.setSelectedIndex(0);
    }

    public void loadPerson(int id, String name, Date birthDate, String gender) {
        idField.setText(String.valueOf(id));
        nameField.setText(name);
        birthDatePicker.setDate(birthDate);
        genderCombo.setSelectedItem(gender);
    }

    public void setEditable(boolean editable) {
        nameField.setEditable(editable);
        birthDatePicker.setEditable(editable);
        genderCombo.setEnabled(editable);
        saveButton.setEnabled(editable);
    }
}